<?php
/**
 * AccountAgentTradeLogsDao
 *
 * @desc use for `account_agent_trade_logs`
 * @author by M.Wang
 */

require_once ('../app/core/models/BaseDao.php');

class AccountAgentTradeLogsDao extends BaseDao {

	private $tableName = 'account_agent_trade_logs';

	public function __construct($dbUtil) {
		parent::__construct ( $dbUtil );
		$this->_name = $this->tableName;
	}

	/**
	 * Get data by account_agent_trade_logs.trade_type [and account_agent_trade_logs.user_id]
	 *
	 * @param int $int_tradeType
	 * @param int $int_userid
	 * @param int $int_page
	 * @param int $int_limit
	 * @return array
	 */
	public function getDataByTradeType($int_tradeType, $int_page, $int_limit, $int_userid = null, $str_otherWhere = null) {

		$str_sql = "
			SELECT SQL_CALC_FOUND_ROWS
			   `a`.*
			FROM `account_agent_trade_logs` AS `a`
			WHERE `a`.`is_delete` = 0
  		";
		if (! ($int_tradeType < 0))
			$str_sql .= " AND `a`.`trade_type` = '{$int_tradeType}' ";
		if (! is_null ( $int_userid ))
			$str_sql .= " AND `a`.`user_id` = '{$int_userid}' ";
		if (! empty ( $str_otherWhere ))
			$str_sql .= $str_otherWhere;
		$str_sql .= 'ORDER BY `a`.`id` DESC LIMIT ' . ($int_page * $int_limit) . ', ' . $int_limit;

		$arr_result = $this->dbUtil->SelectData ( $str_sql, array () );
		if ($arr_result === false) {
			HttpUtil::renderError ( $str_sql );
			exit ();
		}
		return $arr_result;

	}

	/**
	 * get `account_agent_trade_logs` data by id
	 *
	 * @param int $int_id
	 * @return array
	 */
	public function getTradeLogById($int_id) {

		$str_sql = "
			SELECT
				`a`.*, `a`.`id` AS `account_agent_trade_log_id`,
				( SELECT `email` FROM `users` WHERE `id` = `a`.`user_id` ) AS `email`
			FROM `account_agent_trade_logs` AS `a`
			WHERE `a`.`is_delete` = 0
				AND `id` = ?
		";

		return $this->dbUtil->SelectData ( $str_sql, array ($int_id ) );

	}

	/**
	 * get `account_agent_trade_logs` data by orders.id
	 *
	 * @param int $int_id orders.id
	 * @return array
	 */
	public function getTradeLogByOrderId($int_id) {

		$str_sql = "
			SELECT
				`a`.*,
				( SELECT `email` FROM `users` WHERE `id` = `a`.`user_id` ) AS `email`
			FROM `account_agent_trade_logs` AS `a`
			WHERE `a`.`is_delete` = 0
				AND `order_id` = ?
		";

		return $this->dbUtil->SelectData ( $str_sql, array ($int_id ) );

	}

	public function saveAccountAgentTradeLog($data) {
		return $this->insert ( $data );
	}

	public function updateAccountAgentTradeLog($data, $id) {
		$where = " id = $id";
		return $this->update ( $data, $where );
	}

	public function getPaymentBySN($sn) {
		$param = array ($sn );
		$sql = "select * from `$this->tableName` where `is_delete` = 0 and status = 2 and sn = ?";
		$result = $this->dbUtil->SelectData ( $sql, $param );
		return $result;
	}
	
    public function getPaymentBySNWithoutStatus($sn) {
        $param = array ($sn );
        $sql = "select * from `$this->tableName` where `is_delete` = 0 and sn = ?";
        $result = $this->dbUtil->SelectData ( $sql, $param );
        return $result;
    }	
}

?>
